import React, { Component } from 'react';
import InsuranceSearch from './InsuranceSearch';
import InsuranceTable from './InsuranceTable';
import './index.scss';
import api from '../../../api/api';
import { connectRouter } from '../../../mixins/router';
import _ from 'underscore';
class InsuranceOrder2 extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tableData: {},
      searchData: {},
      pageSize:10,
      pageIndex:1
    };
  }
  componentWillMount() {
    this.searchDataFn({});
  }

  searchDataFn = async (data,submitClick) => {
    if(submitClick){
      this.setState({
        searchData:data
      })
    }else{
      data = this.state.searchData
    }
    const pageIndex = submitClick ? 1: this.state.pageIndex
    const pageSize = this.state.pageSize
    const skip = pageSize * (pageIndex - 1);
    const limit = pageSize;
    let tableData  = {}

    // 去除查询空格
    Object.keys(data).forEach(item=>{
      if(data[item]){
        data[item] = data[item].trim()
      }
    })

    tableData.list = await api.getGroupInsuranceList(data, skip, limit + 1);

    if (tableData.list) {
        if (tableData.list.length > limit) {
            tableData.count = skip + limit + 1;
            tableData.list = tableData.list.slice(0, limit);
        } else {
            tableData.count = skip + (tableData.list ? tableData.list.length : 0);
        }
    } else {//兼容不存在list属性的数据结构
        tableData.count = skip + (tableData.length ? tableData.length : 0);
        tableData.list = Array.isArray(tableData) && tableData.slice(0, limit);
    }
    tableData.pageIndex = Number(submitClick ? 1: this.state.pageIndex)
    tableData.pageSize = Number(this.state.pageSize)
    this.setState({
      tableData: tableData,
    });
  };
  pageChange=(pagination)=>{
    // console.log('pagination:',pagination)
    const pageSize = pagination?pagination.pageSize:this.state.pageSize;
    const pageIndex = pagination?pagination.current:this.state.pageIndex;
    this.setState({
      pageSize:pageSize,
      pageIndex:pageIndex
    })
    setTimeout(()=>{
      this.searchDataFn({})
    },0)
  }
  render() {
    return (
      <div>
        <InsuranceSearch searchDataFn={this.searchDataFn}></InsuranceSearch>
        <InsuranceTable
          tableData={this.state.tableData}
          pageChange={this.pageChange}
        ></InsuranceTable>
      </div>
    );
  }
}

export default connectRouter(InsuranceOrder2);
